<?php 
    //验证码
    //设置验证码内的字符
    //参数$length,每次获取几个随机字符
    // function get_str($length=1){
    //     $chars='3456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';//字符库
    //     $s=str_shuffle($chars);//随机打乱字符串
    //     $str=substr($s,0,$length);
    //     return $str;
    // }
	/**
	 * 获得随机字符串
	 * @param   Number  $len    需要的长度
	 * @param   Bool    $special    是否需要特殊符号
	 * @return  String  返回随机字符串
	 */
	function build_ranstrr($len = 8, $special = false)
	{
	    $chars = array(
	        "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
	        "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
	        "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",
	        "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
	        "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2",
	        "3", "4", "5", "6", "7", "8", "9"
	    );
	
	    if ($special) {
	        $chars = array_merge($chars, array(
	            "!", "@", "#", "$", "?", "|", "{", "/", ":", ";",
	            "%", "^", "&", "*", "(", ")", "-", "_", "[", "]",
	            "}", "<", ">", "~", "+", "=", ",", "."
	        ));
	    }
	
	    $charsLen = count($chars) - 1;
	    shuffle($chars);                            //打乱数组顺序
	    $str = '';
	    for ($i = 0; $i < $len; $i++) {
	        $str .= $chars[mt_rand(0, $charsLen)];    //随机取出一位
	    }
	    return $str;
	}
    // echo get_str();
	//生成验证码
	//创建图片
	$width=80;
	$height=34;
	// 创建画布
	$img=imagecreatetruecolor($width,$height);
	//设置背景颜色,颜色必须使用rgb格式
	$bgcolor=imagecolorallocate($img,238,238,238);//#eeeeee
	//设置文字颜色
	$textcolor=imagecolorallocate($img,255,0,0);//#ff0000
	//绘制图片背景,把背景颜色加入图片
	imagefilledrectangle($img,0,0,$width,$height,$bgcolor);
	//第2个和第3个参数是左上角坐标
	//第4个和第5个参数是右下角坐标
	//这两个坐标可以确定一块矩形区域
	//获取验证码字符,每次1个一共获取4个随机字符
	$get_code1=build_ranstrr(1);
	$get_code2=build_ranstrr(1);
	$get_code3=build_ranstrr(1);
	$get_code4=build_ranstrr(1);
	//把验证码放入图片内
	$font='D:/douzhi/PHP/admin/static/foot/OpenSans-Light.ttf';//注意：如果写相对路径，验证码出不来时，把路径写成绝对路径
	imagettftext($img,16,mt_rand(-30,30),5,26,$textcolor,$font,$get_code1);
	imagettftext($img,16,mt_rand(-30,30),20,26,$textcolor,$font,$get_code2);
	imagettftext($img,16,mt_rand(-30,30),40,26,$textcolor,$font,$get_code3);
	imagettftext($img,16,mt_rand(-30,30),60,26,$textcolor,$font,$get_code4);
	//第一个参数是图片变量
	//第二个参数是字体大小
	//第三个参数是字符倾斜度,负数向左,正数向右,数值越大角度越大
	//第四个和第五个参数是字符所在位置的x坐标和y坐标
	//第六个参数是字符颜色
	//第七个参数是字体库
	//第八个参数是需要放进去的字符
	//绘制一些点状像素
	for($i=0;$i<=20;$i++){
	    imagesetpixel($img,mt_rand(0,$width),mt_rand(0,$height),imagecolorallocate($img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255)));
	}
	//第二个和第三个参数是点的位置坐标
	//第四个参数是点的颜色
	
	//绘制一些线像素
	for($i=0;$i<=5;$i++){
	    imageline($img,mt_rand(0,$width),mt_rand(0,$height),mt_rand(0,$width),mt_rand(0,$height),imagecolorallocate($img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255)));
	}
	//第2/3/4/5个参数是线的两端坐标
	//将验证码中的四个字符保存在session里面
	session_start();
	$get_code=$get_code1.$get_code2.$get_code3.$get_code4;
	$_SESSION['imgcode']=$get_code;
	//输出图片
	header('Content-Type:image/png');
	imagepng($img);
	
?>